package cn.honqun.tinycms.service.impl;

import cn.honqun.tinycms.common.exce.ApplicationException;
import cn.honqun.tinycms.constants.SecurityConstants;
import cn.honqun.tinycms.domain.entity.Role;
import cn.honqun.tinycms.repository.RoleMapper;
import cn.honqun.tinycms.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author catstiger
 * @since 2024-02-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public boolean saveOrUpdate(Role entity) {
        //判断角色标识是否存在
        if (baseMapper.isRoleExists(entity)) {
            throw new ApplicationException("角色标识已经存在");
        }
        //调用父类的方法，实现数据库操作
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean removeById(Role entity) {
        //获取完整的角色信息
        entity = baseMapper.selectById(entity.getId());
        //NPE保护
        if (entity == null) {
            throw new ApplicationException("角色不存在");
        }
        //判断是否是super角色
        if (SecurityConstants.ROLE_SUPER.equals(entity.getRole())) {
            throw new ApplicationException("超级管理员角色不可删除");
        }
        return super.removeById(entity);
    }
}
